package com.disruptor.disruptorStudy.demo01;

import java.util.concurrent.atomic.AtomicInteger;

import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.WorkHandler;

//消费者对象
//使用workpool消息处理器,分组消费时(不重复消费),需要实现该接口
// EventHandler: 广播(即重复消费实现此接口)
public class TradeHandler01 implements WorkHandler<Trade>, EventHandler<Trade> {

    private String id;
    //大坑,前面不要用static修饰,否则就会变成类的,而不是某一个单独对象的,这样并发下计算不准确
    private AtomicInteger count = new AtomicInteger(0);

    public TradeHandler01(String id) {
        this.id = id;
    }

    //具体消费数据逻辑
    @Override
    public void onEvent(Trade event) throws Exception {
        System.out.println(this.id + "拿到交易的价格:" + event.getPrice());
        count.incrementAndGet();//记录消费数据总数
    }

    public int getCount() {
        return this.count.get();
    }

    @Override
    public void onEvent(Trade event, long sequence, boolean endOfBatch) throws Exception {
        System.out.println(this.id + "拿到交易的价格:" + event.getPrice());
        count.incrementAndGet();//记录消费数据总数
    }
}
